﻿<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../helpproject.xsl" ?>
<topic template="Default" lasteditedby="Geert" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../helpproject.xsd">
  <title translate="true">Commands</title>
  <keywords>
    <keyword translate="true">Commands</keyword>
    <keyword translate="true">MVVM</keyword>
  </keywords>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Commands</text></para>
    </header>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Commands are supported in Catel. The base class for commands is </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_Command" styleclass="Normal" translate="true">Command</link><text styleclass="Normal" translate="true">.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Code snippets</text></para>
    <list id="1" type="ul" listtype="bullet" formatstring="·" format-charset="SYMBOL_CHARSET" levelreset="true" legalstyle="false" startfrom="1" styleclass="Normal (list)" style="font-family:Symbol; font-size:10pt; color:#000000;">
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">vmcommand - declare a command on a view model</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">vmcommandwithcanexecute - declare a command with support for CanExecute on a view model</text></li>
    </list>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Explanation</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">To implement commands, and still be able to unit test the view models, a separate command is introduced. This command allows a developer to implement a command that can be invoked both via code (unit testing) and UI.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">There is one real Command class: </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_Command`2" styleclass="Normal" translate="true">Command&lt;TCanExecuteParameter, TExecuteParameter&gt;</link></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">The </text><text styleclass="Normal" style="font-style:italic;" translate="true">TCanExecuteParameter </text><text styleclass="Normal" translate="true">is the parameter that is passed to the </text><link displaytype="text" defaultstyle="true" type="weblink" href="http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.canexecute.aspx" target="_blank" styleclass="Normal" translate="true">CanExecute</link><text styleclass="Normal" translate="true"> of the command, ands saves the developer from casting the object (as in the interface </text><link displaytype="text" defaultstyle="true" type="weblink" href="http://msdn.microsoft.com/en-us/library/ms616869.aspx" target="_blank" styleclass="Normal" translate="true">ICommand</link><text styleclass="Normal" translate="true"> to a typed object). The same goes for </text><text styleclass="Normal" style="font-style:italic;" translate="true">TExecuteParameter</text><text styleclass="Normal" translate="true"> which makes the </text><link displaytype="text" defaultstyle="true" type="weblink" href="http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.execute.aspx" target="_blank" styleclass="Normal" translate="true">Execute</link><text styleclass="Normal" translate="true"> of the command typed.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">There are also several wrappers available in case object is used as type parameter:</text></para>
    <para styleclass="Normal"><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_Command`1" styleclass="Normal" translate="true">Command&lt;TExecuteParameter&gt;</link><text styleclass="Normal" translate="true"> (wraps </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_Command`2" styleclass="Normal" translate="true">Command&lt;TCanExecuteParameter, TExecuteParameter&gt;</link><text styleclass="Normal" translate="true"> with </text><text styleclass="Normal" style="font-style:italic;" translate="true">object</text><text styleclass="Normal" translate="true"> for </text><text styleclass="Normal" style="font-style:italic;" translate="true">TCanExecuteParameter</text><text styleclass="Normal" translate="true">)</text></para>
    <para styleclass="Normal"><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_Command" styleclass="Normal" translate="true">Command</link><text styleclass="Normal" translate="true"> (wraps </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_Command`1" styleclass="Normal" translate="true">Command&lt;TExecuteParameter&gt;</link><text styleclass="Normal" translate="true"> with </text><text styleclass="Normal" style="font-style:italic;" translate="true">object</text><text styleclass="Normal" translate="true"> for </text><text styleclass="Normal" style="font-style:italic;" translate="true">TExecuteParameter</text><text styleclass="Normal" translate="true">)</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Examples</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Code:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">void</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> MyViewModel()</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    </text><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">// Add commands</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    MyAction = </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">new</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> Command(MyAction_Execute);</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">}</text><br/><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// &lt;summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// Gets the MyAction command.</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// &lt;/summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> Command MyAction { get; </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">private</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> set; }</text><br/><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// &lt;summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// Method to invoke when the MyAction command is executed.</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// &lt;/summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">/// &lt;param name=&quot;parameter&quot;&gt;The parameter of the command.&lt;/param&gt;</text><br/><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">private</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">void</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> MyAction_Execute(</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">object</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> parameter)</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    </text><text styleclass="Code Example" style="font-weight:normal; font-style:italic; text-decoration:none; color:#800000;" translate="true">// Show message box</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    var messageService = GetService&lt;IMessageService&gt;();</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    messageService.ShowInfo(&quot;My action in MVVM&quot;);</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">}</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Xaml (assuming that the view model is set as datacontext):</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">&lt;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">Button</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true"> Content=&quot;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">Click me</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">&quot; Command=&quot;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">{Binding MyCommand}</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">&quot; /&gt;</text></para>
  </body>
</topic>
